/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Visao;

import RMI.ServicosIF;
import RMI.ServicosImplementacao;
import de.javasoft.plaf.synthetica.SyntheticaSimple2DLookAndFeel;
import de.javasoft.plaf.synthetica.SyntheticaStandardLookAndFeel;
import java.rmi.NoSuchObjectException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.ExportException;
import java.rmi.server.UnicastRemoteObject;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.UIManager;

/**
 *
 * @author kassio
 */
public class AppServidor extends javax.swing.JFrame {
    
    ServicosIF servicos;
    Registry registro;
    


    /**
     * Creates new form AppServidor
     */
    public AppServidor() {
        initComponents();
        setLocationRelativeTo( null );        
       

        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel4 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        btIniciar = new javax.swing.JButton();
        btParar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Hotéis Hilton");
        setResizable(false);

        jLabel4.setText("IFPB");

        jLabel2.setText("Programação com objetos distribuídos");

        jLabel3.setText("Profº. Thiago Moura");

        jLabel1.setText("Servidor da Aplicação - Hotéis Hilton");

        btIniciar.setText("Iniciar");
        btIniciar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btIniciarActionPerformed(evt);
            }
        });

        btParar.setText("Parar");
        btParar.setEnabled(false);
        btParar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btPararActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(20, 20, 20)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(6, 6, 6)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel3)
                            .add(jLabel2)
                            .add(jLabel1)
                            .add(layout.createSequentialGroup()
                                .add(jLabel4)
                                .add(218, 218, 218))))
                    .add(layout.createSequentialGroup()
                        .add(btIniciar)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(btParar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 83, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(19, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(18, 18, 18)
                .add(jLabel4)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jLabel2)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jLabel3)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jLabel1)
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(btIniciar)
                    .add(btParar))
                .addContainerGap(21, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btPararActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btPararActionPerformed
        try { 
            UnicastRemoteObject.unexportObject(registro,true);
 
        }  
        catch(NoSuchObjectException e) {
            JOptionPane.showMessageDialog(null, "O serviço já foi finalizado!");
        }
        catch( Exception e ) {  
            
            JOptionPane.showMessageDialog(null, "ERRO: "+ e);
        }  
                
        btIniciar.setEnabled(true);
        btParar.setEnabled(false);
    }//GEN-LAST:event_btPararActionPerformed

    private void btIniciarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btIniciarActionPerformed

        try { 
            servicos = ServicosImplementacao.getInstance();  
            registro = LocateRegistry.createRegistry(1099);
            registro.rebind("hoteishilton", servicos);
 
        }  
        catch( ExportException e ) {  
            
            JOptionPane.showMessageDialog(null, "O servidor já foi iniciado!");
        }        
        catch( Exception e ) {  
            
            JOptionPane.showMessageDialog(null, "ERRO: "+ e);
        }  
                
        btIniciar.setEnabled(false);
        btParar.setEnabled(true);
    }//GEN-LAST:event_btIniciarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        
        try {
            

            UIManager.setLookAndFeel(new SyntheticaSimple2DLookAndFeel());
            

        } catch (Exception ex) {
            Logger.getLogger(AppServidor.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new AppServidor().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btIniciar;
    private javax.swing.JButton btParar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    // End of variables declaration//GEN-END:variables
}
